package com.eastmelon.blog.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.eastmelon.blog.dao.UserDao;
import com.eastmelon.blog.entity.User;
import com.eastmelon.blog.service.UserService;
import com.eastmelon.blog.util.uploadUtil;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Resource(name = "userDao")
	private UserDao userDao;

	@Override
	public User get(Serializable id) {
		return userDao.get(id);
	}

	@Override
	public void save(User entity) {
		userDao.save(entity);

	}

	@Override
	public void update(User entity) {
		userDao.update(entity);

	}

	@Override
	public void uploadUserIamge(User user, InputStream is, String oldImageName) {
		try {
			if (is != null) {
				uploadUtil.uploadImage(is, user.getImageName(), 80, 80);
				if (oldImageName != null && !"".equals(oldImageName)) {
					uploadUtil.deleteImage(oldImageName);
				}
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		userDao.update(user);
	}

	@Override
	public String getImageName(Serializable id) {
		return userDao.getImageName(id);
	}

	@Override
	public User getUserByUserName(String username) {
		return userDao.getUserByuserName(username);
	}

	@Override
	public String getUserInfor(Serializable id) {
		return userDao.getUserInfor(id);
	}

}
